package com.mk.admin.service.impl;

import com.mk.admin.mapper.SysMenuMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.entity.SysMenuEntity;
import com.mk.admin.model.vo.SysMenuVo;
import com.mk.admin.service.SysMenuService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public ResultDto select(SysMenuVo sysMenuVo) {
        return ResultDto.builder()
                .result(sysMenuMapper.select(JSONCopy.copy(sysMenuVo, SysMenuEntity.class)))
                .count(sysMenuMapper.count(JSONCopy.copy(sysMenuVo, SysMenuEntity.class)))
                .retCode("T")
                .build();
    }

    @Override
    public ResultDto add(SysMenuVo sysMenuVo) {
        sysMenuVo.setCreateTime(DateTool.format());
        return ResultDto.data(sysMenuMapper.insert(JSONCopy.copy(sysMenuVo, SysMenuEntity.class)));
    }

    @Override
    public ResultDto update(SysMenuVo sysMenuVo) {
        sysMenuVo.setUpdateTime(DateTool.format());
        return ResultDto.data(sysMenuMapper.updateById(JSONCopy.copy(sysMenuVo, SysMenuEntity.class)));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(sysMenuMapper.deleteById(id));
    }


    @Override
    public ResultDto status(int id) {
        return ResultDto.data(sysMenuMapper.status(id));
    }

    @Override
    public ResultDto deleteBatchIds(List<SysMenuVo> list) {
        List<Integer> ids = list.stream().map(SysMenuVo::getId).collect(Collectors.toList());
        return ResultDto.data(sysMenuMapper.deleteBatchIds(ids));
    }
}
